package com.lgj.web.sys.controller;

import com.github.pagehelper.PageInfo;
import com.lgj.core.LoginUser;
import com.lgj.result.ResultBody;
import com.lgj.web.sys.model.*;
import com.lgj.web.sys.service.SysRoleService;
import com.lgj.web.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Controller
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService roleService;

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody<SysUser> register(@Validated @RequestBody SysUserParam SysUserParam) {
        SysUser SysUser = sysUserService.register(SysUserParam);
        if (SysUser == null) {
            return ResultBody.fail();
        }
        return ResultBody.success(SysUser);
    }

    @ResponseBody
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public ResultBody getUserInfo(Principal principal) {
//        if(principal==null){
//            return ResultBody.unauthorized(null);
//        }
        String username = principal.getName();
        SysUser SysUser = sysUserService.getAdminByUsername(username);
        Map<String, Object> data = new HashMap<>();
        data.put("username", SysUser.getUsername());
        data.put("menus", roleService.getMenuList(SysUser.getId()));
        data.put("icon", SysUser.getIcon());
        List<SysRole> roleList = sysUserService.getRoleList(SysUser.getId());
        if(!CollectionUtils.isEmpty(roleList)){
            List<String> roles = roleList.stream().map(SysRole::getName).collect(Collectors.toList());
            data.put("roles",roles);
        }
        return ResultBody.success(data);
    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultBody<PageInfo> list(@RequestParam(value = "keyword", required = false) String keyword,
                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        System.out.println("这是生产环境-------------------------------------------");
        return ResultBody.success(sysUserService.list(keyword, pageSize, pageNum));
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResultBody<SysUser> getItem(@PathVariable Long id) {
        SysUser admin = sysUserService.getItem(id);
        return ResultBody.success(admin);
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody update(@PathVariable Long id, @RequestBody SysUser admin) {
        int count = sysUserService.update(id, admin);
        if (count > 0) {
            return ResultBody.success(count);
        }
        return ResultBody.fail();
    }

    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody updatePassword(@Validated @RequestBody UpdateUserPasswordParam updatePasswordParam) {
        int status = sysUserService.updatePassword(updatePasswordParam);
        if (status > 0) {
            return ResultBody.success(status);
        } else if (status == -1) {
            return ResultBody.fail("提交参数不合法");
        } else if (status == -2) {
            return ResultBody.fail("找不到该用户");
        } else if (status == -3) {
            return ResultBody.fail("旧密码错误");
        } else {
            return ResultBody.fail();
        }
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody delete(@PathVariable Long id) {
        int count = sysUserService.delete(id);
        if (count > 0) {
            return ResultBody.success(count);
        }
        return ResultBody.fail();
    }

    @RequestMapping(value = "/updateStatus/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody updateStatus(@PathVariable Long id,@RequestParam(value = "status") Integer status) {
        SysUser SysUser = new SysUser();
        SysUser.setStatus(status);
        int count = sysUserService.update(id,SysUser);
        if (count > 0) {
            return ResultBody.success(count);
        }
        return ResultBody.fail();
    }

    @RequestMapping(value = "/role/update", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody updateRole(@RequestParam("userId") Long userId,
                                   @RequestParam("roleIds") List<Long> roleIds) {
        int count = sysUserService.updateRole(userId, roleIds);
        if (count >= 0) {
            return ResultBody.success(count);
        }
        return ResultBody.fail();
    }

    @RequestMapping(value = "/role/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public ResultBody<List<SysRole>> getRoleList(@PathVariable Long userId) {
        List<SysRole> roleList = sysUserService.getRoleList(userId);
        return ResultBody.success(roleList);
    }

    @ResponseBody
    @RequestMapping(value = "/get_by_username", method = RequestMethod.GET)
    public ResultBody<LoginUser> getUserByUserName(String userName){
        return ResultBody.success(sysUserService.getUserByUserName(userName));
    }

}
